home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Found / FWRunTyp / FWClaInf.h < prev    next >
Encoding:
Text File  |  1996-09-17  |  6.2 KB  |  177 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWClaInf.h
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #ifndef FWCLAINF_H
  11. #define FWCLAINF_H
  12.  
  13. #ifndef FWCLAIMP_H
  14. #include "FWClaImp.h"
  15. #endif
  16.  
  17. #ifdef FW_COMPILER_SUPPORTS_RTTI
  18. #include <typeinfo.h>
  19. #endif
  20.  
  21. #ifndef FW_COMPILER_SUPPORTS_RTTI
  22. inline int operator==(const FW_SClassInfo& first, const FW_SClassInfo& second)
  23.     { return &first == &second; }
  24. #endif
  25.  
  26. #ifndef FW_COMPILER_SUPPORTS_RTTI
  27. inline int operator!=(const FW_SClassInfo& first, const FW_SClassInfo& second)
  28.     { return &first != &second; }
  29. #endif
  30.  
  31. //========================================================================================
  32. // FW_PrivStaticGetClassInfo
  33. //========================================================================================
  34.  
  35. #ifndef FW_COMPILER_SUPPORTS_RTTI
  36. template <class T>
  37. inline FW_SClassInfoPtr FW_PrivStaticGetClassInfo(T*)
  38. {
  39.     return &T::fgClassInfo;
  40. }
  41. #endif
  42.  
  43. //========================================================================================
  44. // FW_PrivVirtualGetClassInfo
  45. //========================================================================================
  46.  
  47. #ifndef FW_COMPILER_SUPPORTS_RTTI
  48. template <class T>
  49. inline FW_SClassInfoPtr FW_PrivVirtualGetClassInfo(const T* p)
  50. {
  51.     return p ? p->PrivVirtualGetClassInfo() : 0;
  52. }
  53. #endif
  54.  
  55. //
  56. // [JEL] Reminder by KVV:
  57. //
  58. //    Probably need to change DYNAMIC_CAST so its syntax is like in the ANSI C++ draft, i.e.:
  59. //
  60. //    A* a = ...
  61. //    const B* b = dynamic_cast<const B*>(a);
  62. //
  63. //  JEL, 12/14/95: KVV is referring to the problem with const.  I've tweaked
  64. //    things a bit and it's now possible to write:
  65. //
  66. //    const B* b = FW_DYNAMIC_CAST(const B, a);
  67.  
  68. #ifndef FW_COMPILER_SUPPORTS_RTTI
  69. template <class tTo, class tFrom>
  70. inline void* FW_DynamicCast(tTo* to, tFrom* from)
  71. {
  72.     return FW_PrivDynamicCast(from, 
  73.                             FW_PrivVirtualGetClassInfo(from), 
  74.                             FW_PrivStaticGetClassInfo(from), 
  75.                             FW_PrivStaticGetClassInfo(to));
  76. }
  77. #endif
  78.  
  79. //========================================================================================
  80. // Macros for generating and accessing class information
  81. //========================================================================================
  82.  
  83. // ----- The public macros for declaring and defining RTTI scaffolding
  84.  
  85. #ifdef FW_COMPILER_SUPPORTS_RTTI
  86.     #define FW_DECLARE_CLASS
  87.     #define FW_DEFINE_CLASS_M0(name)
  88.     #define FW_DEFINE_CLASS_M1(name, ancestor)
  89.     #define FW_DEFINE_CLASS_M2(name, ancestor1, ancestor2)
  90.     #define FW_DEFINE_CLASS_M3(name, ancestor1, ancestor2, ancestor3)
  91.     #define FW_DEFINE_CLASS_M4(name, ancestor1, ancestor2, ancestor3, ancestor4)
  92. #else
  93.     #ifndef   FWCLAPRV_H
  94.         #include "FWClaPrv.h"
  95.     #endif
  96.     #define FW_DECLARE_CLASS                                                                \
  97.         virtual FW_SClassInfoPtr PrivVirtualGetClassInfo(void) const;                        \
  98.         FW_SClassInfoPtr PrivNonVirtualGetClassInfo(void) const                                \
  99.             { return &fgClassInfo; }                                                            \
  100.         static FW_SClassInfo fgClassInfo;                                                    \
  101.         static const FW_SClassInfoPtr fgAncestors[];                                            \
  102.         static const size_t fgAncestorOffsets[];
  103.     
  104.     #define FW_DEFINE_CLASS_M0(name)                                                        \
  105.         _FW_CLASS_ANCESTORS_IMPLEMENT_M0(name)                                                \
  106.         _FW_DEFINE_CLASSINFO_OBJECT(name)                                                    \
  107.         _FW_CLASSINFO_ACCESSOR_MEMBERS(name)
  108.     
  109.     #define FW_DEFINE_CLASS_M1(name, ancestor)                                                \
  110.         _FW_CLASS_ANCESTORS_IMPLEMENT_M1(name, ancestor)                                    \
  111.         _FW_DEFINE_CLASSINFO_OBJECT(name)                                                    \
  112.         _FW_CLASSINFO_ACCESSOR_MEMBERS(name)
  113.     
  114.     #define FW_DEFINE_CLASS_M2(name, ancestor1, ancestor2)                                    \
  115.         _FW_CLASS_ANCESTORS_IMPLEMENT_M2(name, ancestor1, ancestor2)                        \
  116.         _FW_DEFINE_CLASSINFO_OBJECT(name)                                                    \
  117.         _FW_CLASSINFO_ACCESSOR_MEMBERS(name)
  118.     
  119.     #define FW_DEFINE_CLASS_M3(name, ancestor1, ancestor2, ancestor3)                        \
  120.         _FW_CLASS_ANCESTORS_IMPLEMENT_M3(name, ancestor1, ancestor2, ancestor3)                \
  121.         _FW_DEFINE_CLASSINFO_OBJECT(name)                                                    \
  122.         _FW_CLASSINFO_ACCESSOR_MEMBERS(name)
  123.     
  124.     #define FW_DEFINE_CLASS_M4(name, ancestor1, ancestor2, ancestor3, ancestor4)            \
  125.         _FW_CLASS_ANCESTORS_IMPLEMENT_M4(name, ancestor1, ancestor2, ancestor3, ancestor4)    \
  126.         _FW_DEFINE_CLASSINFO_OBJECT(name)                                                    \
  127.         _FW_CLASSINFO_ACCESSOR_MEMBERS(name)
  128. #endif
  129.  
  130. //========================================================================================
  131. // RTTI Portability Macros
  132. //
  133. //    These macros provide a transition path until all compilers support RTTI.
  134. //========================================================================================
  135.  
  136. //----------------------------------------------------------------------------------------
  137. // Dynamic Cast Macro
  138. //----------------------------------------------------------------------------------------
  139.  
  140. #ifdef FW_COMPILER_SUPPORTS_RTTI
  141.     #define FW_DYNAMIC_CAST(T, p) dynamic_cast<T*>(p)
  142. #else
  143.     #define FW_DYNAMIC_CAST(T, p) (T*) FW_DynamicCast((T*)0, p)
  144. #endif
  145.  
  146. //----------------------------------------------------------------------------------------
  147. // typeid macros
  148. //----------------------------------------------------------------------------------------
  149.  
  150. // Note, C++ typeid's are TypeInfo *references*.
  151. // ODF TYPEIDs are FW_ClassInfo *references*
  152. // Internally, ODF usually uses FW_SClassInfoPtrs, which are are FW_ClassInfo *pointers*.
  153.  
  154. #ifdef FW_COMPILER_SUPPORTS_RTTI
  155.     typedef type_info FW_TypeInfo;
  156.     #define FW_TYPEID_FROM_TYPE(type) (typeid(type))
  157.     #define FW_TYPEID_FROM_POINTER(p) (typeid(*(p)))
  158. #else
  159.     typedef FW_SClassInfo FW_TypeInfo;
  160.     #define FW_TYPEID_FROM_TYPE(type) (*FW_CLASS_INFO_PTR(type))
  161.     #define FW_TYPEID_FROM_POINTER(p) (*(p)->PrivVirtualGetClassInfo())
  162. #endif
  163.  
  164. //----------------------------------------------------------------------------------------
  165. // Class name macros
  166. //----------------------------------------------------------------------------------------
  167.  
  168. #ifdef FW_COMPILER_SUPPORTS_RTTI
  169.     #define FW_CLASSNAME_FROM_TYPE(type) (typeid(type).name())
  170.     #define FW_CLASSNAME_FROM_POINTER(p) (typeid(*(p)).name())
  171. #else
  172.     #define FW_CLASSNAME_FROM_TYPE(type) (FW_TYPEID_FROM_TYPE(type).fClassName)
  173.     #define FW_CLASSNAME_FROM_POINTER(p) (FW_TYPEID_FROM_POINTER(p).fClassName)
  174. #endif
  175.  
  176. #endif
  177.